package com.qianyitian.hope2.analyzer.util;

import com.qianyitian.hope2.analyzer.config.Constant;
import com.qianyitian.hope2.analyzer.model.KLineInfo;
import com.qianyitian.hope2.analyzer.model.Stock;

import java.util.ArrayList;
import java.util.DoubleSummaryStatistics;
import java.util.List;
import java.util.stream.Collectors;

public class BarUtil {
    public static void moveToMiddlePosition(List<Number[]> numbers) {
        DoubleSummaryStatistics collect = numbers.stream().collect(Collectors.summarizingDouble(number -> (Double) number[1]));
        double max=collect.getMax();
        double min=collect.getMin();
        double middle=(max+min)/2;
        numbers.stream().forEach(number -> number[1] = (Double) number[1] - middle );
    }

    //转成chart 能显示的格式
    public static List<Number[]> convert2ChartFormatOnlyClosePrice(Stock stock) {
        List<Number[]> collect = stock.getkLineInfos().stream().map(bar -> {
            long dateMilliSeconds = Constant.ONE_DAY_MILLISECONDS * bar.getDate().toEpochDay();
            Number[] row = new Number[]{dateMilliSeconds, bar.getClose()};
            return row;
        }).collect(Collectors.toList());
        return collect;
    }


    public static List<KLineInfo> getRecentKLine(Stock stock, int days) {
        if (stock.getkLineInfos().size() < days + 1) {
            return stock.getkLineInfos();
        }

        int size = stock.getkLineInfos().size();
        List<KLineInfo> kLineInfos = stock.getkLineInfos().subList(size - days, size);
        List<KLineInfo> result = new ArrayList<>();
        result.addAll(kLineInfos);
        return result;

    }
}
